export default {
    name: "GoodList",
    data() {
        return {
            goodList: [],
            queryInfo: {
                pagenum: 1,
                pagesize: 6,
                totalCount: 0,
            },
            filterData: {
                name: null,
                cat_one_id: null,
                order: []
            },
            loading: false,
            finished: false,
            error: false,
            refreshing: false,
            score: 0,
            sales: 0,
            price: 0,
            scoreOption: [
                { text: '默认排序', value: null },
                { text: '人气升序', value: 'score' },
                { text: '人气降序', value: '-score' },
            ],
            salesOption: [
                { text: '默认排序', value: null },
                { text: '销量升序', value: 'sales' },
                { text: '销量降序', value: '-sales' },
            ],
            priceOption: [
                { text: '默认排序', value: null },
                { text: '价格升序', value: 'price' },
                { text: '价格降序', value: '-price' },
            ],
        }
    },
    created() {
        this.filterData.cat_one_id = this.$route.params.catId;
        this.filterData.name = this.$route.query.name;
        console.log(this.$route);
    },
    methods: {
        onLoad() {
            this.error = false;
            let pagenum = this.queryInfo.pagenum;
            let pagesize = this.queryInfo.pagesize;
            let api = this.$http.api.good.getPage(pagenum, pagesize);
            this.$http.callApi(api, {
                data: this.filterData
            }).then(res => {
                if (this.refreshing) {
                    this.goodList = [];
                    this.refreshing = false;
                }
                this.goodList.push(...res.data.good);
                if (this.goodList.length >= res.data.total) {
                    this.finished = true;
                } else {
                    this.queryInfo.pagenum++;
                }
                this.loading = false;
            }).catch(err => {
                this.loading = false;
                this.error = true;
            });
        },
        onRefresh() {
            this.queryInfo.pagenum = 1;
            this.goodList = [];
            this.onLoad();
        },
        onDropChange(type, val) {
            // 多字段排序
            let order = this.filterData.order;
            let exsit_index;
            if (val != null) {
                exsit_index = order.findIndex(it => it.indexOf(type) > -1);
                exsit_index == -1 && order.push(val);
                exsit_index > -1 && order.splice(exsit_index, 1, val);
            } else {
                exsit_index = order.findIndex(it => it.indexOf(type) > -1);
                exsit_index > -1 && order.splice(exsit_index, 1);
            }
            console.log(order);
            // 重新获取数据
            this.onRefresh();
        }
    }
}